//
//  KHHSearchCollectionViewController.m
//  Travel
//
//  Created by dllo on 15/12/2.
//  Copyright © 2015年 doll. All rights reserved.
//

#import "KHHSearchCollectionViewController.h"
#import "KHHSearchCollectionViewCell.h"
#import "KHHSearchTitleCollectionReusableView.h"
#import "KHHSearchViewController.h"
#import "CXDestinationDetailViewController.h"
#import "CXDestinationCityListViewController.h"
#import "KHHRecommendModel.h"

@interface KHHSearchCollectionViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UISearchBarDelegate>
@property (nonatomic, retain)UICollectionView *collectionView;
@property (nonatomic, retain)UICollectionViewFlowLayout *layout;
@property (nonatomic, retain)UISearchBar *searchBar;

@end

@implementation KHHSearchCollectionViewController


#pragma mark - 视图将要消失
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.searchBar resignFirstResponder];
    self.searchBar.hidden = YES;
}

//即将出现
- (void)viewWillAppear:(BOOL)animated
{
    self.searchBar.hidden = NO;
    [self.searchBar resignFirstResponder];
 //   [self.view endEditing:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor colorWithRed:0.98 green:0.97 blue:0.93 alpha:1];
    [self createView];
    [self SearchBar];
    [self addScreenEdgePan];
   
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"leftBack"] style:UIBarButtonItemStylePlain target:self action:@selector(backToLast)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
}




- (void )createView
{
    self.layout = [[UICollectionViewFlowLayout alloc]init];
    
    self.layout.itemSize = CGSizeMake((CWIDTH - 40) / 3, 30);
    self.layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    self.layout.headerReferenceSize = CGSizeMake(CWIDTH, CWIDTH / 7);
    
    //创建collectionView
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, self.view.bounds.size.height) collectionViewLayout:self.layout];
    //滑动视图距离根视图内容边界
    // self.collectionView.contentInset = UIEdgeInsetsMake(10, 10, 10, 10);
    self.collectionView.backgroundColor = [UIColor colorWithRed:0.98 green:0.97 blue:0.93 alpha:1];
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.view addSubview:self.collectionView];
    
    //collectionView 注册cell
    [self.collectionView registerClass:[KHHSearchCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [self.collectionView registerClass:[KHHSearchTitleCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
}



#pragma mark - 创建searchBar
- (void)SearchBar
{
    // 初始化
    self.searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(60, 5, WIDTH - 80, 30)];
    // 设置代理
    self.searchBar.delegate = self;
    //类型
    [self.searchBar setBarStyle:UIBarStyleDefault];
    //提示文本
    self.searchBar.placeholder = @"搜索目的地  游记...";
    
    //键盘类型
    [self.searchBar setKeyboardType:UIKeyboardTypeDefault];
    
    [self.searchBar becomeFirstResponder];
    //视图剪切
    self.searchBar.layer.masksToBounds = YES;
    self.searchBar.layer.cornerRadius = 10;
    
    //关闭用户交互
    self.searchBar.userInteractionEnabled = YES;
    [self.navigationController.navigationBar addSubview:self.searchBar];
    
    
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
    [self.searchBar resignFirstResponder];
}

#pragma mark - searchBar 触发方法
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [self.searchBar resignFirstResponder];
    KHHSearchViewController *search = [[KHHSearchViewController alloc] init];
    search.text = self.searchBar.text;
    [self.navigationController pushViewController:search animated:YES];
    
}

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
    return YES;
}

#pragma mark - 按钮触发方法
- (void)backToLast
{
    [self.navigationController popViewControllerAnimated:YES];
    [self.searchBar removeFromSuperview];
}

#pragma mark - 分区数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (kind == UICollectionElementKindSectionHeader) {
        KHHSearchTitleCollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
        header.model = self.modelSearchArr[indexPath.section];
        return header;
    }
    return nil;
}

#pragma mark - 分行数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [self.modelSearchElementsArr[section] count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    KHHSearchCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.model = self.modelSearchElementsArr[indexPath.section][indexPath.item];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSNumber *type = ((KHHRecommendModel *)self.modelSearchElementsArr[indexPath.section][indexPath.row]).type;
    NSString *str_valueId = [NSString stringWithFormat:@"%@", [self.modelSearchElementsArr[indexPath.section][indexPath.row]notesId]];
    
    if ([type integerValue] == 3 || [str_valueId isEqualToString:@"HK"] || [str_valueId isEqualToString:@"MO"])
    {
        CXDestinationDetailViewController *detail = [[CXDestinationDetailViewController alloc] init];
        detail.type = ((KHHRecommendModel *)self.modelSearchElementsArr[indexPath.section][indexPath.row]).type ;
        detail.valueId = [NSString stringWithFormat:@"%@", [self.modelSearchElementsArr[indexPath.section][indexPath.row]notesId]];
        
        [self.navigationController pushViewController:detail animated:YES];

        
    } else {

        CXDestinationCityListViewController *city = [[CXDestinationCityListViewController alloc] init];
        city.type = ((KHHRecommendModel *)self.modelSearchElementsArr[indexPath.section][indexPath.row]).type ;
        city.valueId = [NSString stringWithFormat:@"%@", [self.modelSearchElementsArr[indexPath.section][indexPath.row]notesId]];
        
        [self.navigationController pushViewController:city animated:YES];
    }
    

}

#pragma mark-滑动手势1
- (void)addScreenEdgePan
{
    UIScreenEdgePanGestureRecognizer *screenEdgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(presentLeftMenuViewController:)];
    screenEdgePan.edges = UIRectEdgeLeft;
    [self.view addGestureRecognizer:screenEdgePan];
    [self.collectionView addGestureRecognizer:screenEdgePan];

}
#pragma mark-滑动手势2
-(void)presentLeftMenuViewController:(UIScreenEdgePanGestureRecognizer *)edge
{
    [self.navigationController popViewControllerAnimated:YES];
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
